package ru.vms.healthcontrol;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import pro.notum.interfaces.Command;
import pro.notum.interfaces.IPresenter;
import pro.notum.interfaces.IView;
import pro.notum.mobile.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class NotumView implements IView {

	Activity activ = null;
	IPresenter presenter = null;
	TextView user_date = null;
	Button fix_date = null;
	
	ValueUnderControl value = null;
	Date date = null;
	EditText edit_value = null;
	private ProgressDialog progressDialog = null;
	private ImageView indicator;
	private TextView list_val;
	
	
	final public void updateDateView () {
		user_date.setText(new SimpleDateFormat("dd.MM.yyyy").format(date));
		indicator.setImageResource(presenter.isExistValuesForDate(date)?android.R.drawable.presence_online:android.R.drawable.presence_busy);
	}
	
	public NotumView(IPresenter presenter) {
		activ = presenter.getActivity();
		this.presenter = presenter;
		date = new Date();
		
		
		ImageButton save = (ImageButton) activ.findViewById(R.id.save_values);
		save.setOnClickListener( new View.OnClickListener() {
			
			public void onClick(View v) {
				value.setValue( Float.parseFloat(edit_value.getText().toString()) );
				value.setModify(date);
				NotumView.this.presenter.saveBtnPressed();
				updateDateView();
			}
		});
		
		ImageButton sign_in_out = (ImageButton) activ.findViewById(R.id.sign_in_out);
		sign_in_out.setOnClickListener( new View.OnClickListener() {
			
			public void onClick(View v) {
				NotumView.this.presenter.signInOut();
			}
		});
		
		this.indicator = (ImageView) this.activ.findViewById(R.id.data_exist);
		
		this.list_val = (TextView) this.activ.findViewById(R.id.textListValues);
		
		initDatePicker();
	
		printUserData();
		
		printValue();
		
		initEditUserPrefButtons();
	}

	public void initEditUserPrefButtons() {
		//
		// TO MODIFY PREFERENCES
		// 
		
		View.OnClickListener edit_pref = new View.OnClickListener() {
			
			public void onClick(View v) {
				activ.startActivity(new Intent(activ, PrefScreen.class));
			}
		};
		
		ImageButton fix_usr_data = (ImageButton) activ.findViewById(R.id.go_fix_user_data);
		fix_usr_data.setOnClickListener( edit_pref );
		
		ImageButton fix_usr_data2 = (ImageButton) activ.findViewById(R.id.go_sign_in_dialog);
		fix_usr_data2.setOnClickListener(edit_pref);
	}

	public void initDatePicker() {
		//
		// DATE
		// 
		user_date = (TextView) activ.findViewById(R.id.user_date);
		updateDateView();
		
		user_date.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				
				final int year = date.getYear()+1900;
				final int month = date.getMonth();
				final int day = date.getDate();
				
				DatePickerDialog dlg = new DatePickerDialog (activ, new DatePickerDialog.OnDateSetListener() {
					
					public void onDateSet(DatePicker view, int year, int monthOfYear,
							int dayOfMonth) {
						date = new Date(year-1900,monthOfYear,dayOfMonth);
						updateDateView();
					}
				},year,month,day );
				
				dlg.show();
			}
		});
	}

	public void printValue() {
		value = this.presenter.loadValuesForControl(this.date);
		
		edit_value = (EditText) activ.findViewById(R.id.edit_value);
		TextView name_value = (TextView) activ.findViewById(R.id.name_value);
		TextView dimension_value = (TextView) activ.findViewById(R.id.dimension_value);
	
		name_value.setText(value.getDescr());
		edit_value.setText(value.getValueString());
		dimension_value.setText(value.getDimension());
		
		Button inc = (Button) activ.findViewById(R.id.inc_value);
		Button dec = (Button) activ.findViewById(R.id.dec_value);
		
		inc.setOnClickListener( new View.OnClickListener() {
			public void onClick(View v) {
				value.setValue( Float.parseFloat(edit_value.getText().toString()) );
				value.increment();
				edit_value.setText(value.getValueString());
			}
		});
		
		dec.setOnClickListener( new View.OnClickListener() {
			
			public void onClick(View v) {
				value.setValue( Float.parseFloat(edit_value.getText().toString()) );
				value.decrement();
				edit_value.setText(value.getValueString());
			}
		});
		
		
	}
	

	public void printUserData() {
		UserData user = this.presenter.getUserData();
		
		String text;
		if( user.isLoginComplete() ) {
			text = String.format("%s [%s]", activ.getString(R.string.login_complete_notum), user.getNick());
		} else 
			text = activ.getString(R.string.need_signin_notum);
		
		TextView login_info = (TextView) this.activ.findViewById(R.id.login_complete_info);
		login_info.setText(text);
		
		
		TextView email = (TextView) activ.findViewById(R.id.user_email);
		TextView nick = (TextView) activ.findViewById(R.id.user_nick);
		

		email.setText(user.getLogin());
		nick.setText(user.getNick());

		
		TextView sex = (TextView) activ.findViewById(R.id.user_sex);
		TextView height = (TextView) activ.findViewById(R.id.user_height);
		TextView age = (TextView) activ.findViewById(R.id.user_age);
		
		sex.setText(user.getSexStr());
		height.setText(user.getHeihtStr());
		age.setText(user.getAgeStr());
	}


	public void updateUserInfo() {
		printUserData();
	}

	public ValueUnderControl getModifiedValues() {
		return value;
	}

	public Date getDate() {
		return date;
	}

	public void AskUserToUpdate(final Command positiveAction) {

		AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this.activ);                      
	    dlgAlert.setTitle(this.activ.getString(R.string.attention)); 
	    dlgAlert.setMessage(this.activ.getString(R.string.rewrite_data)); 
	    dlgAlert.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
					positiveAction.doCommand();			
			}
		});
	    dlgAlert.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
			}
		});
	    
	    dlgAlert.setCancelable(false);
	    dlgAlert.create().show();
	}
	
	
	public void AskUserToChangeSettings(final String message) {

		AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this.activ);                      
	    dlgAlert.setTitle(this.activ.getString(R.string.attention)); 
	    dlgAlert.setMessage(String.format("%s\n%s", message, this.activ.getString(R.string.change_settings))); 
	    dlgAlert.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
					NotumView.this.activ.startActivity(new Intent(activ, PrefScreen.class));
			}
		});
	    dlgAlert.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
			}
		});
	    
	    dlgAlert.setCancelable(false);
	    dlgAlert.create().show();
	}
	
	

	public void printError(String errortext) {
		NetLog.MsgBox(activ, activ.getString(R.string.attention),"%s",errortext);
	}

	public void beginWaitDialog(String message) {
		progressDialog = ProgressDialog.show(activ, "", message);
		
	}

	public void stopWaitDialog() {
		if( progressDialog != null )
			progressDialog.dismiss();
		
	}

	public void selectTab(String tabId) {
		
		if( tabId.equals(MainActivity.GRAPH_TAB)) {
			
			StringBuilder sb = new StringBuilder();
			
			List<ValueUnderControl> lst = presenter.selectByName(ValueUnderControl.WEIGHT_NAME);
			if( lst == null)
				return;
			
			for (ValueUnderControl v : lst) {
				sb.append(v.toString() + "\n");
			}
			
			
			list_val.setText( sb.toString() );
		} // ... else if ...
		
	}

}
